﻿using System;
using Microsoft.Phone.BackgroundAudio;
using Samples.Radio.Agent.Model;
using Samples.Radio.Data;

namespace Samples.Radio
{
    public class TrackViewModel : NotifyBase
    {
        private string _TrackName;
        public string TrackName
        {
            get
            {
                return _TrackName;
            }
            set
            {
                _TrackName = value;
                RaisePropertyChanged(()=>TrackName);
            }
        }

        private string _Artist;
        public string Artist
        {
            get
            {
                return _Artist;
            }
            set
            {
                _Artist = value;
                RaisePropertyChanged(()=>Artist);
            }
        }

        private string _Album;
        public string Album
        {
            get
            {
                return _Album;
            }
            set
            {
                _Album = value;
                RaisePropertyChanged(()=>Album);
            }
        }

        private string _AlbumArt;
        public string AlbumArt
        {
            get
            {
                return _AlbumArt;
            }
            set
            {
                _AlbumArt = value;
                RaisePropertyChanged(()=>AlbumArt);
                RaisePropertyChanged(()=>AlbumArtUri);
            }
        }

        public Uri AlbumArtUri
        {
            get
            {
                if (string.IsNullOrEmpty(this.AlbumArt))
                {
                    return null;
                }
                else
                {
                    if (this.AlbumArt.StartsWith("http", StringComparison.InvariantCultureIgnoreCase))
                    {
                        return new Uri(this.AlbumArt);
                    }
                    else
                    {
                        return new Uri(this.AlbumArt, UriKind.Relative);
                    }
                }
            }
        }

        public TrackViewModel()
        { }

        public TrackViewModel(AudioTrack track)
        {
            ChangeTrack(track);
        }

        public TrackViewModel(PlaylistTrack track)
        {
            ChangeTrack(track);
        }

        public void ChangeTrack(AudioTrack track)
        {
            this.TrackName = track.Title;
            this.Artist = track.Artist;
            this.Album = track.Album;

            
            //this.AlbumArt = track.AlbumArt.AbsolutePath;
        }

        public void ChangeTrack(PlaylistTrack track)
        {
            this.TrackName = track.Title;
            this.Artist = track.Artist;
            this.Album = track.Album;
            this.AlbumArt = track.AlbumArtUrl;
        }

        public string TrackImageUrl
        {
            get { return AlbumArt; }
        }
    }
}